package cms.mager.service.impl;


import cms.common.CodeMsg;
import cms.common.PageData;
import cms.common.Result;
import cms.mager.dao.UserDao;
import cms.mager.domain.User;
import cms.mager.service.UserService;

import java.util.List;

/**
 * 功能:
 * Author: yuhui
 * Date: 2021/4/8 17:02
 */
public class UserServiceImpl implements UserService {
    UserDao u_dao = new UserDao();

    @Override
    public Result Login(String name, String pwd) {
        Result rt = null;
        if(name != "" && name != null && pwd != "" && pwd != null){
            User user= u_dao.findOne(name,pwd);
            if(user == null){
                rt = new Result(CodeMsg.ERROR_USER_NOTFOUND); //此用户不存在
            }else{
                rt = new Result(user); //正确响应
            }
        }else{
            rt = new Result(CodeMsg.ERROR_USER_NAME_PWD); //用户名或密码不正确
        }
        return rt;
    }

    @Override
    public Result findUserPage(int page, int pageSize, PageData pd) {
        Result rt = null;
        try{
            PageData<User> userPage = u_dao.findUserPage(page, pageSize, pd);
            rt = new Result(userPage);
        }catch(Exception e){
            e.printStackTrace();
            rt = new Result(CodeMsg.ERROR_SERVER_BUG);
            return rt;
        }
        return rt;
    }

    /**
    * 功能描述: 全查询用户数据
    * @Author: yuhui  2021/4/8 17:28
    */
    public Result findAllUser(){
        Result re = null;
        try{
            List<User> all = u_dao.findAll();
            re = new Result(all);
        }catch(Exception e){
            e.printStackTrace();
            re = new Result(CodeMsg.ERROR_SERVER_BUG);
            return re;
        }
        return re;
    }

    @Override
    public Result adduser(Object[] arr) {
        Result rt = null;
        if(u_dao.addData(arr)){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_ADD_USER);
        }
        return rt;
    }

    @Override
    public Result reSetPwd(int id,String pwd) {
        Result rt = null;
        if(u_dao.reset(id,pwd)){
            rt = new Result();
        }else {
            rt = new Result(CodeMsg.ERROR_SET_USER);
        }
        return rt;
    }

    @Override
    public Result delInfoById(int id) {
        Result rt = null;
        if(u_dao.delUserById(id)){
            rt = new Result();
        }else {
            rt = new Result(CodeMsg.ERROR_DEL_USER);
        }
        return rt;
    }

    @Override
    public Result getAllName() {
        List<User> allName = u_dao.getAllName();
        return new Result(allName);
    }

    @Override
    public Result updPwd(String id,String newPwd) {
        Result rt = null;
        if(u_dao.reset(Integer.valueOf(id),newPwd)){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_MODIFY_IMG);
        }
        return rt;
    }

    @Override
    public Result updImg(String imgFile, Integer id) {
        Result rt = null;
        if(u_dao.updImg(id,imgFile)){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_SERVER_BUG);
        }
        return rt;
    }

}
